/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MidiGui;

import java.util.Scanner;
import midi.Metronome;
import midi.MidiPlayer;
import music.Chords.Chord;
import music.types.MidiNote;
import music.types.NoteTypes;

/**
 *
 * @author TJ
 */
public class ChordPlayerTest {
    public static void main(String[] args) throws Exception{
        MidiPlayer m=MidiPlayer.getMidiPlayer();
        Scanner sc=new Scanner(System.in);
        String ch=sc.nextLine();
        Chord c=null;
        while(true) {
            if(ch.equals("key")) {
                new Frame();
            } else if(ch.equals("construct")) {
                ChordConstructor.createFrame();
            }else if(ch.startsWith("m ") || ch.equals("m")) {
                try{
                Scanner sc2=new Scanner(ch.substring(2));
                boolean dostart=false,dostop=false;
                int bpmset=-1;
                while(sc2.hasNext()) {
                    String arg=sc2.next();
                    if(arg.equals("stop")){
                        dostop=true;
                    } else if(arg.equals("start")){
                        dostart=true;
                    }else if(arg.equals("-b")){
                        bpmset=Integer.parseInt(sc2.next());
                    }
                }
                if(bpmset>0) {Metronome.getMetronome().setBPM(bpmset);}
                if(dostart) {Metronome.getMetronome().start();}
                if(dostop) {Metronome.getMetronome().stop();}
                } catch(Exception e) {
                    System.out.println("Metronome command used inccorectly");
                }
                
            }else if(ch.equals("exit")) {
                System.exit(0);
            }else if(ch.isEmpty() && c!=null){
                playChord(c,m);
            } else{
                try{
                    c=Chord.parse(NoteTypes.WHOLE,ch);
                    playChord(c,m);
                }catch(Exception e) {System.out.println("invalid command");}
            }
            ch=sc.nextLine();
        }
    }
    public static void playChord(Chord c,MidiPlayer m) {
        writeChord(c);
        m.playNote(c, 4000);
    }
    public static void writeChord(Chord c) {
        System.out.print("Now Playing: ");
        for(MidiNote n : c.getNotes()) {
            System.out.print(n.getName()+", ");
        }
        System.out.println();
    }
    
}
